package com.xiaoye.qbdatahandle.service;

import com.huaban.analysis.jieba.JiebaSegmenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TextAnalyzerService {
    @Autowired
    TextHandleService textHandleService;

    // 统计文本中每个单词的频率
    public Map<String, Integer> analyzeText(String text) {
        if (text == null || text.isEmpty()) {
            return new HashMap<>();
        }

        List<String> words = textHandleService.analyseText(text);

        // 统计词频
        Map<String, Integer> wordFrequency = new HashMap<>();

        for (String word : words) {
            if (word.length() > 1 && !TextHandleService.STOP_WORDS.contains(word)) {  // 去除停用词和单个字符的词
                wordFrequency.put(word, wordFrequency.getOrDefault(word, 0) + 1);
            }
        }

        return wordFrequency;
    }
}
